﻿///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2009 David Hill. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
///////////////////////////////////////////////////////////////////////////////
using System;
using System.ComponentModel;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Events;

using TFSBranchVisualizer.BranchVisualizer.Models;
using TFSBranchVisualizer.BranchVisualizer.Services;
using Infrastructure.TFSServiceClient;

namespace TFSBranchVisualizer.BranchVisualizer.ViewModels
{
    /// <summary>
    /// ViewModel for View1.
    /// </summary>
    public class ViewModel1 : INotifyPropertyChanged
    {
        private readonly IEventAggregator _eventAggregator;

        public ViewModel1(IDataService dataService, IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;

            // Use the data service to load the model.
            dataService.BranchLoadingComplete += new EventHandler<BranchLoadingEventArgs>(dataService_BranchLoadingComplete);
            dataService.BranchLoadingError += new EventHandler<BranchLoadingErrorEventArgs>(dataService_BranchLoadingError);
            dataService.GetBranches("$/OnlineProviderDirectory/Main");

            // Initialize this ViewModel's commands.
            Command1 = new DelegateCommand<string>(ExecuteCommand1, CanExecuteCommand1);
        }

        void dataService_BranchLoadingError(object sender, BranchLoadingErrorEventArgs e)
        {
            //TODO: handle errors gracefully.
        }

        void dataService_BranchLoadingComplete(object sender, BranchLoadingEventArgs e)
        {
            Trunk = e.Results;
        }


        private BranchInfo trunk;
        public BranchInfo Trunk 
        {
            get
            {
                return trunk;
            }
            private set
            {
                trunk = value;
                NotifyPropertyChanged("Trunk");
            }
        }

        private BranchInfo selectedChildBranch;
        public BranchInfo SelectedChildBranch
        {
            get
            {
                return selectedChildBranch;
            }
            set
            {
                selectedChildBranch = value;
                NotifyPropertyChanged("SelectedChildBranch");
            }
        }

        #region Command1
        public DelegateCommand<string> Command1 { get; private set; }

        private void ExecuteCommand1(string commandParameter)
        {
        }

        private bool CanExecuteCommand1(string commandParameter)
        {
            return true;
        }

        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
